<template>
    <div class="app-container">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
        <el-form-item label="楼层名称" prop="title">
          <el-input v-model="queryParams.title" placeholder="请输入楼层名称" clearable
            @keyup.enter.native="handleQuery" />
        </el-form-item>
        <!-- <el-form-item label="客户端类型" prop="clientType">
          <el-select v-model="queryParams.clientType" placeholder="客户端类型" clearable>
            <el-option v-for="dict in dict.type.client_client_type" :key="dict.value"
              :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item> -->
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索
          </el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
            v-hasPermi="['setting:floor:add']">新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single"
            @click="handleUpdate" v-hasPermi="['setting:floor:edit']">修改</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>
      <el-table border v-loading="loading" :data="floorSetList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="ID" align="center" prop="id" width="200" />
        <el-table-column label="楼层名称" align="center" prop="title" :show-overflow-tooltip="true">
        </el-table-column>
        <el-table-column label="客户端类型" prop="clientType" align="center" width="250">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.client_client_type" :value="scope.row.clientType" />
          </template>
        </el-table-column>
        <el-table-column label="状态" align="center" prop="enabled">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.enabled" @change="handleEnabledChange(scope.row)">
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column label="顺序" align="center" prop="orderNum" width="200" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
              v-hasPermi="['setting:floor:edit']">修改</el-button>
            <el-button size="mini" type="text" icon="el-icon-circle-plus-outline"
              @click="handleAddData(scope.row)" v-hasPermi="['setting:floor:add']"
              v-if="scope.row.floorSign=='VILLA'?true:false">添加子楼层</el-button>
              <el-button size="mini" type="text" icon="el-icon-edit" @click="openCard(scope.row)"
              >卡片管理</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize" @pagination="getList" />

      <!-- 添加或修改协议对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="400px" append-to-body>
        <el-form ref="form" :model="form" label-width="90px">
          <el-form-item label="客户端类型" prop="clientType">
            <el-select v-model="form.clientType" placeholder="客户端类型" clearable disabled>
              <el-option v-for="dict in dict.type.client_client_type" :key="dict.value"
                :label="dict.label" :value="dict.value" />
            </el-select>
          </el-form-item>
          <el-form-item label="楼层标记" prop="floorSign">
            <el-select v-model="form.floorSign" placeholder="楼层标记" clearable>
              <el-option v-for="dict in dict.type.app_floor_sign" :key="dict.value"
                :label="dict.label" :value="dict.value" />
            </el-select>
          </el-form-item>
          <el-form-item label="活动类型" prop="promotionType">
            <el-select v-model="form.promotionType" placeholder="活动类型" clearable>
              <el-option v-for="dict in dict.type.app_promotion_type" :key="dict.value"
                :label="dict.label" :value="dict.value" />
            </el-select>
          </el-form-item>
          <el-form-item label="楼层名称" prop="title">
            <el-input v-model="form.title" />
          </el-form-item>
          <el-form-item label="更多链接" prop="moreLink">
            <el-input v-model="form.moreLink" />
          </el-form-item>
          <el-form-item label="显示顺序" prop="orderNum">
            <el-input-number v-model="form.orderNum" controls-position="right" :min="0" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
      <CardDialog :floorId ='floorId' @handleClose="handleClose" clientType="WECHAT_MP" :cardOpen="cardOpen"/>
    </div>
  </template>

  <script>
  import { listFloorSet, delFloorSet, addFloorSet, updateFloorSet, handleEnabledChange } from "@/api/mallAdmin/floorSet/floorSet";
  import CardDialog from '../CardDialog.vue'
  export default {
    components:{
        CardDialog
    },
    name: "FloorSet",
    dicts: ['client_client_type', 'app_floor_sign', 'app_promotion_type'],
    data () {
      return {
        floorId: '',
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 协议表格数据
        floorSetList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        cardOpen:false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          title: undefined,
          createBy: undefined,
          classify: undefined,
          clientType:'WECHAT_MP'
        },
        // 表单参数
        form: {
            noticeId: undefined,
            noticeTitle: undefined,
            noticeType: undefined,
            noticeContent: undefined,
            status: "0",
            clientType:'WECHAT_MP'
        },
        // 表单校验
        rules: {
          title: [
            { required: true, message: "协议标题不能为空", trigger: "blur" }
          ],
          classify: [
            { required: true, message: "协议类型不能为空", trigger: "change" }
          ]
        }
      };
    },
    created () {
      this.getList();
    },
    methods: {
      handleClose(){
        this.cardOpen = false;
      },
      openCard(row){
        console.log(row)
        this.floorId = row.id;
        this.cardOpen = true;
      },
      /** 查询协议列表 */
      getList () {
        this.loading = true;
        listFloorSet(this.queryParams).then(response => {
          this.floorSetList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel () {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset () {
        this.form = {
          noticeId: undefined,
          noticeTitle: undefined,
          noticeType: undefined,
          noticeContent: undefined,
          status: "0",
          clientType:'WECHAT_MP'
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery () {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery () {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange (selection) {
        this.ids = selection.map(item => item.noticeId)
        this.single = selection.length != 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd () {
        this.reset();
        this.open = true;
        this.title = "添加楼层";
      },
      /** 修改按钮操作 */
      handleUpdate (row) {
        this.reset();
        this.form = row;
        this.open = true;
        this.title = "修改楼层";
      },
      /** 修改启用状态*/
      handleEnabledChange (row) {
        let text = row.enabled === true ? "启用" : "禁用";
        this.$modal.confirm('确认"' + text + '""' + row.title + '"吗？').then(function () {
          return handleEnabledChange(row.id, row.enabled);
        }).then(() => {
          this.$modal.msgSuccess(text + "成功");
        }).catch(function () {
          row.enabled = !row.enabled;
        });
      },
      /** 提交按钮 */
      submitForm: function () {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != undefined) {
              updateFloorSet(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addFloorSet(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 添加子楼层操作 */
      handleAddData (row) {
        // this.title = "添加子楼层";
        this.$router.push({
          // name: 'floorSetData',
          // query: {id:row.id}
          path: '/floorSet-data/' + row.id
        })

      },
    },

  };
  </script>
